package com.derbysoft.synxisadapter.dswitch2.translator.impl;

import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.generic.constants.TransactionStatusType;
import com.derbysoft.remote.dswitch.generic.constants.UniqueIDType;
import com.derbysoft.remote.dswitch.generic.dto.UniqueIDDTO;
import com.derbysoft.remote.dswitch.hotel.dto.CancelInfoDTO;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRQ;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRS;
import com.derbysoft.remote.dswitch.hotel.dto.HotelReservationIDDTO;
import com.derbysoft.synxisadapter.commons.dto.synxis.OTACancelRS;
import com.derbysoft.synxisadapter.dswitch2.translator.Translator;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CancelRSTranslatorImpl implements Translator<List<OTACancelRS>, GenericRS<CancelRS>, GenericRQ<CancelRQ>> {
    public static final String SEPARATOR = ";";

    @Override
    public GenericRS<CancelRS> translate(List<OTACancelRS> otaCancelRSs, GenericRQ<CancelRQ> rq) {
        return new GenericRS<CancelRS>(null, translatePayload(otaCancelRSs, rq.getPayload()));
    }

    private CancelRS translatePayload(List<OTACancelRS> otaCancelRSs, CancelRQ cancelRQ) {

        CancelRS cancelRS = new CancelRS();
        cancelRS.setResStatusType(TransactionStatusType.CANCELLED);
        ArrayList<HotelReservationIDDTO> hotelReservationIDDTOs = new ArrayList<HotelReservationIDDTO>();

        hotelReservationIDDTOs.add(TranslateHelper.findId(cancelRQ.getHotelReservationIDs(), UniqueIDType.ERSP));

        StringBuilder cancellationIds = new StringBuilder();
        for (OTACancelRS otaCancelRS : otaCancelRSs) {
            cancellationIds.append(otaCancelRS.getCancelInfoRS().getUniqueID().getID()).append(SEPARATOR);
        }
        cancelRS.setCancelInfo(new CancelInfoDTO(new UniqueIDDTO(StringUtils.removeEnd(cancellationIds.toString(), SEPARATOR), UniqueIDType.CANCELLATION)));

        hotelReservationIDDTOs.add(new HotelReservationIDDTO(UniqueIDType.CANCELLATION,
                StringUtils.removeEnd(cancellationIds.toString(), SEPARATOR)));
        cancelRS.setHotelReservationIDs(hotelReservationIDDTOs);

        return cancelRS;
    }

}